package cqrtplm.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.hustcad.plm.basic.util.TyplmLoggerUtil;
import com.hustcad.plm.pdm.ec.mapper.TyplmChgActRecordLinkMapper;
import com.hustcad.plm.pdm.ec.module.vo.ChgActivityVO;
import com.hustcad.plm.pdm.ec.service.TyplmChangeLinkService;
import com.hustcad.plm.pdm.ec.service.TyplmChgNoticeQueryService;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowObjectPackageLink;
import com.hustcad.plm.pdm.flowbusiness.service.TyplmFlowObjectPackageService;
import com.hustcad.plm.pdm.mpm.model.dto.book.param.TyplmTechnologyBookCardParam;
import com.hustcad.plm.pdm.mpm.model.dto.capp.TyplmTechnologyBookCardWFObjInfo;
import com.hustcad.plm.pdm.mpm.service.impl.capp.TyplmCappMpmCardServiceImpl;
import com.hustcad.plm.pdm.workflow.model.entity.WorkFlowContainer;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowContainerService;
import com.ty.basic.constant.TableTypeConstrant;
import com.ty.basic.entity.change.ChgActRecordLinkDO;
import com.ty.basic.entity.change.ChgNoticeDO;
import com.ty.basic.util.TySpringBeanUtil;
import cqrtplm.constant.RTPlmConstant;
import cqrtplm.mapper.RTProcessMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

@Service
@Primary
@Slf4j
public class RTTyplmCappMpmCardServiceImpl extends TyplmCappMpmCardServiceImpl {

    @Resource
    @Lazy
    private TyplmWorkFlowContainerService typlmWorkFlowContainerService;
    @Resource
    @Lazy
    private TyplmFlowObjectPackageService typlmFlowObjectPackageService;
    @Resource
    private RTProcessMapper processMapper;

    public List<TyplmTechnologyBookCardWFObjInfo> getMpmObjectListByWf(TyplmTechnologyBookCardParam wfInfo) {
        List<TyplmTechnologyBookCardWFObjInfo> params = new ArrayList<>();
        if (ObjectUtil.isEmpty(wfInfo)) {
            log.info("工艺成册-getMpmObjectListByWf ==> 入参为空 ");
            TyplmLoggerUtil.debug(log, () -> "getMpmObjectListByWf  ==>  入参为空", () -> new Object[0]);
        } else {
            log.info("工艺成册-getMpmObjectListByWf  ==>  入参 =【{}】", JSON.toJSONString(wfInfo));
            WorkFlowContainer workFlowContainer = this.typlmWorkFlowContainerService.selectOneByOid(
                    wfInfo.getWorkFlowContainerId());
            switch (workFlowContainer.getBaseObjectOtype()) {
                case "com.ty.entity.workFlow.FlowObjectPackage":
                    params.addAll(this.getFromFlowObjectPackage2(wfInfo.getWorkFlowContainerId()));
                    break;
                case "ty.inteplm.change.CTyChangeActivity":
                    params.addAll(this.getFromChangeActivity2(workFlowContainer.getBaseObjectOid()));
                    break;
                case "ty.inteplm.change.CTyChangeNotice":
                    params.addAll(this.getFromChangeNotice2(workFlowContainer.getBaseObjectOid()));
            }
            log.info("工艺成册-getMpmObjectListByWf  ==>  返回值 =【{}】", JSON.toJSONString(params));
        }
        return params;
    }


    //如果是工艺路线 查询下二级
    //如果四工艺阶段 查询下一级
    private List<TyplmTechnologyBookCardWFObjInfo> getFromFlowObjectPackage2(BigInteger workFlowContainerId) {
        List<TyplmTechnologyBookCardWFObjInfo> dtoList = new ArrayList<>();
        List<FlowObjectPackageLink> flowObjectPackageLinkList = this.typlmFlowObjectPackageService.getWorkFlowObject(
                workFlowContainerId);

        log.info("工艺成册-getFromFlowObjectPackage  <== 入参=【{}】,返回数据后续跳过非工艺对象与typeMarkFlag不为1的对象=【{}】 ",
                 workFlowContainerId, JSON.toJSONString(flowObjectPackageLinkList));

        Set<BigInteger> planOidList = new HashSet<>();
        Set<BigInteger> operationOidList = new HashSet<>();
        for (FlowObjectPackageLink flowObjectPackageLink : flowObjectPackageLinkList) {
            if (TableTypeConstrant.TY_MPMOPERATION_TYPE.equals(flowObjectPackageLink.getRefObjectOtype())) {
                operationOidList.add(flowObjectPackageLink.getRefObjectOid());
            }else if(TableTypeConstrant.TY_MPMPROCESSPLAN_TYPE.equals(flowObjectPackageLink.getRefObjectOtype())){
                planOidList.add(flowObjectPackageLink.getRefObjectOid());
            }
        }

        getCardWFObjInfoList(planOidList, dtoList, operationOidList);
        return dtoList;
    }

    private void getCardWFObjInfoList(Set<BigInteger> planOidList, List<TyplmTechnologyBookCardWFObjInfo> dtoList,
                           Set<BigInteger> operationOidList) {
        if(CollUtil.isNotEmpty(planOidList)){
            //根据工艺路线OID 查询工序
            List<TyplmTechnologyBookCardWFObjInfo> list = processMapper.selectProcessByPlanOid(planOidList);
            if(CollUtil.isNotEmpty(list)){
                dtoList.addAll(list);
            }
        }

        if(CollUtil.isNotEmpty(operationOidList)){
            //根据工艺阶段OID 查询工序
            List<TyplmTechnologyBookCardWFObjInfo> list = processMapper.selectTypeByOperationOid(operationOidList);
            if(CollUtil.isNotEmpty(list)){
                List<BigInteger> stageOidList = new ArrayList<>();
                for (TyplmTechnologyBookCardWFObjInfo info : list){
                    String otype = info.getOtype();
                    if(RTPlmConstant.TY_MPMOPERATION_TYPE_PROCESSSEQUENCE.equals(otype)){
                        dtoList.add(info);
                    }else if(RTPlmConstant.TY_MPMOPERATION_TYPE_PROCESSSTAGE.equals(otype)){
                        stageOidList.add(info.getOid());
                    }
                }
                if(CollUtil.isNotEmpty(stageOidList)){
                    List<TyplmTechnologyBookCardWFObjInfo> stageList = processMapper.selectProcessByStageOid(stageOidList);
                    if(CollUtil.isNotEmpty(stageList)){
                        dtoList.addAll(stageList);
                    }
                }
            }
        }
    }

    private List<TyplmTechnologyBookCardWFObjInfo> getFromChangeActivity2(BigInteger ecaOid) {
        TyplmChgActRecordLinkMapper chgActRecordLinkMapper = TySpringBeanUtil.getBean(
                TyplmChgActRecordLinkMapper.class);
        List<ChgActRecordLinkDO> chgActRecordLinkDOList = chgActRecordLinkMapper.queryObjOidAndOtypeByECAId(ecaOid);

        log.info("工艺成册-getFromChangeActivity  ==> 入参=【{}】,返回数据后续跳过非工艺对象=【{}】 ", ecaOid,
                 JSON.toJSONString(chgActRecordLinkDOList));

        Set<BigInteger> planOidList = new HashSet<>();
        Set<BigInteger> operationOidList = new HashSet<>();
        for (ChgActRecordLinkDO chgActRecordLinkDO : chgActRecordLinkDOList) {
            if (TableTypeConstrant.TY_MPMOPERATION_TYPE.equals(chgActRecordLinkDO.getBotype())) {
                operationOidList.add(chgActRecordLinkDO.getBoid());
            }else if(TableTypeConstrant.TY_MPMPROCESSPLAN_TYPE.equals(chgActRecordLinkDO.getBotype())){
                planOidList.add(chgActRecordLinkDO.getBoid());
            }
        }
        List<TyplmTechnologyBookCardWFObjInfo> dtoList = new ArrayList<>();
        getCardWFObjInfoList(planOidList, dtoList, operationOidList);
        return dtoList;
    }

    private List<TyplmTechnologyBookCardWFObjInfo> getFromChangeNotice2(BigInteger ecnOid) {
        List<TyplmTechnologyBookCardWFObjInfo> dtoList = new ArrayList<>();
        TyplmChgNoticeQueryService tyChgNoticeQueryService = TySpringBeanUtil.getBean(TyplmChgNoticeQueryService.class);
        ChgNoticeDO chgNoticeDO = tyChgNoticeQueryService.selectByPrimaryKey(ecnOid);
        if (!Objects.isNull(chgNoticeDO)) {
            TyplmChangeLinkService tyChangeLinkService = TySpringBeanUtil.getBean(TyplmChangeLinkService.class);
            List<ChgActivityVO> chgActivityList = tyChangeLinkService.queryECAByECO(chgNoticeDO);
            log.info("getFromChangeActivity  <== 入参=【{}】,返回数据后续跳过非工艺对象=【{}】 ", chgNoticeDO,
                     JSON.toJSONString(chgActivityList));

            for (ChgActivityVO chgActivityVO : chgActivityList) {
                List<TyplmTechnologyBookCardWFObjInfo> ecaDtoList = this.getFromChangeActivity2(chgActivityVO.getOid());
                if (!ObjectUtil.isEmpty(ecaDtoList)) {
                    dtoList.addAll(ecaDtoList);
                }
            }

        }
        return dtoList;
    }

}
